package com.cssw.fyzb.tps.service.auth.tecent;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.tps.exception.BizException;
import com.cssw.fyzb.tps.util.HttpUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static com.cssw.fyzb.tps.util.Bizs.AUTH_RESULT_QUERY_FAIL;


/**
 * @Author zhouxu
 * @create 2020-09-14 21:41
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AuthResult {

    private static final Logger logger = LoggerFactory.getLogger(NONCETicket.class);

    private String url;
    private String appId;
    private String version;
    private String getFile;

    public HttpResult getResultInfo(String orderNo, String sign, String nonce) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("app_id", appId);
        param.put("version", version);
        param.put("order_no", orderNo);
        param.put("nonce", nonce);
        param.put("sign", sign);
        param.put("get_file", getFile);

        logger.debug("人脸核身结果查询入参：" + JSON.toJSONString(param));

        long startTime = System.currentTimeMillis();
        String json = HttpUtil.get(url, param);
        logger.debug("人脸核身结果查询返回：" + json);
        long endTime = System.currentTimeMillis();
        logger.debug("useTime: " + (endTime - startTime));

        HttpResult result = JSON.parseObject(json, HttpResult.class);
        if (result == null || !HttpResult.STATUS_SUCCESS.equals(result.code)) {
            logger.error(json);
            String errMsg = null;
            if (result != null)
                errMsg = result.getMsg();
            throw new BizException(AUTH_RESULT_QUERY_FAIL.code, AUTH_RESULT_QUERY_FAIL.message + errMsg);
        }

        return result;
    }

    /**
     * 出参报文：
     * {
     * "code": "0",
     * "msg": "请求成功",
     * "bizSeqNo": "18081020001015300215034200859792",
     * "result": {
     * "bizSeqNo":"18081020001015300215034200859792",
     * "transactionTime":"20180810150342",
     * "orderNo":"orderNo19959248596551",
     * "idNo":"** *",
     * "idType":"01",
     * "name":"** *",
     * "video":"** ** **",
     * "photo":"** ** **",
     * "liveRate":"100",
     * "similarity":"95.0",
     * "occurredTime":"20180810150152",
     * "success": false
     * },
     * "transactionTime":"20180810150342",
     * "app_id":"TIDAlsRT",
     * "order_no":"orderNo19959248596551"
     * }
     */
    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class HttpResult {

        public static final String STATUS_SUCCESS = "0";

        private String code;
        private String msg;
        private String bizSeqNo;
        private String orderNo;
        private String appId;
        private String transactionTime;
        private Data data;

    }

    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    private class Data {
        private String bizSeqNo;
        private String transactionTime;
        private String orderNo;
        private String idNo;
        private String idType;
        private String name;
        private String video;
        private String photo;
        private String liveRate;
        private String similarity;
        private String occurredTime;
        private boolean success;

    }

}
